<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="群号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.groupNumber`" :rules="formRules.groupNumber" class="mb-0px!">
            <el-input v-model="row.groupNumber" placeholder="请输入群号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="群名" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.groupName`" :rules="formRules.groupName" class="mb-0px!">
            <el-input v-model="row.groupName" placeholder="请输入群名" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.status`" :rules="formRules.status" class="mb-0px!">
            <el-select v-model="row.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.YK_QQ_GROUP_POOL_STATE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="来源" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.source`" :rules="formRules.source" class="mb-0px!">
            <el-select v-model="row.source" placeholder="请选择来源">
                <el-option
                  v-for="dict in getIntDictOptions(DICT_TYPE.YK_QQ_GROUP_POOL_SOURCE)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="池子名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.poolName`" :rules="formRules.poolName" class="mb-0px!">
            <el-input v-model="row.poolName" placeholder="请输入池子名称" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="使用次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taskCount`" :rules="formRules.taskCount" class="mb-0px!">
            <el-input v-model="row.taskCount" placeholder="请输入使用次数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="成功次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.successCount`" :rules="formRules.successCount" class="mb-0px!">
            <el-input v-model="row.successCount" placeholder="请输入成功次数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="失败次数" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.failureCount`" :rules="formRules.failureCount" class="mb-0px!">
            <el-input v-model="row.failureCount" placeholder="请输入失败次数" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="使用时间" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.lastUseTime`" :rules="formRules.lastUseTime" class="mb-0px!">
            <el-date-picker
              v-model="row.lastUseTime"
              type="date"
              value-format="x"
              placeholder="选择使用时间"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="使用设备ID" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.deviceId`" :rules="formRules.deviceId" class="mb-0px!">
            <el-input v-model="row.deviceId" placeholder="请输入使用设备ID" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="任务ID" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.taskId`" :rules="formRules.taskId" class="mb-0px!">
            <el-input v-model="row.taskId" placeholder="请输入任务ID" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="使用QQ" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qqAccount`" :rules="formRules.qqAccount" class="mb-0px!">
            <el-input v-model="row.qqAccount" placeholder="请输入使用QQ" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加群号池群号</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { GroupPoolApi } from '@/api/yunkong/grouppool'

const props = defineProps<{
  poolId: number // 群号池ID（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref<any[]>([])
const formRules = reactive({
  poolId: [{ required: true, message: '群号池ID不能为空', trigger: 'blur' }],
  groupNumber: [{ required: true, message: '群号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.poolId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await GroupPoolApi.getGroupPoolGroupListByPoolId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    poolId: undefined,
    groupNumber: undefined,
    groupName: undefined,
    status: undefined,
    source: undefined,
    poolName: undefined,
    remark: undefined,
    taskCount: undefined,
    successCount: undefined,
    failureCount: undefined,
    lastUseTime: undefined,
    deviceId: undefined,
    taskId: undefined,
    qqAccount: undefined
  }
  row.poolId = props.poolId as any
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>